package main.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Relation extends Component implements Cloneable {

	private List<Entity> entities = new ArrayList<Entity>();
	private Map<Entity, Cardinality> cardinalities = new HashMap<Entity, Cardinality>();
	private Boolean isComposition = Boolean.FALSE;
	
	public Relation(String name){
		super(name);
	}
	
	public Relation() {
		super();
	}
	
	public void addEntity(Entity entity){
		this.getEntities().add(entity);
	}

	public List<Entity> getEntities() {
		return entities;
	}
	
	public void setCardinalityToEntity(Entity entity, Cardinality cardinality){
		this.cardinalities.put(entity, cardinality);
	}
	
	public Cardinality getCardinalityForEntity(Entity entity){
		return this.cardinalities.get(entity);
	}
	
	public Entity getEntityByName(String name){
		Iterator<Entity> iterator = this.entities.iterator();
		Boolean found = Boolean.FALSE;
		Entity e = null;
		while (iterator.hasNext() && !found){
			Entity next = iterator.next();
			if (next.getName().equals(name)){
				e = next;
			}
		}
		return e;
	}
	
	public Boolean isRelation(){
		return Boolean.TRUE;
	}
	

	public Boolean isComposition() {
		return isComposition;
	}

	public void setIsComposition(Boolean isComposition) {
		this.isComposition = isComposition;
	}

	public void removeCardinalities() {
		this.cardinalities = new HashMap<Entity, Cardinality>();
	}
	
	public Boolean removeEntity(Entity e){
		if (this.cardinalities.remove(e) != null){
			return this.entities.remove(e);
		}
		return false;
	}

}
